package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CategoryServicelmpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public Result page(CategoryPageQueryDTO dto) {
        //非空判断
        if (dto.getPage() == null) {
            dto.setPage(1);
        }
        if (dto.getPageSize() == null) {
            dto.setPageSize(5);
        }
        //开启分页
        PageHelper.startPage(dto.getPage(), dto.getPageSize());

        //调用mapper
        Page<Category> page = categoryMapper.page(dto);
        //封装
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());

        return Result.success(pageResult);
    }

    /**
     * 启用，禁用状态 	1为启用，0为禁用
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public Result status(Integer status, Long id) {
        //非空判断
        if (status == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        if (id == null) {
            return Result.error(MessageConstant.ACCOUNT_NULL);
        }
        //调用mapper
        Category category = new Category();
        //set 状态 and id
        category.setStatus(status);
        category.setId(id);
        //set 修改时间 修改人
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.updateStatus(category);

        return Result.success();
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result deleteById(Long id) {
        //查询当前分类是否关联了菜品
        Integer count = dishMapper.countByCategoryId(id);
        if (count>0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        count= setmealDishMapper.countByCategoryId(id);
        if (count>0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //调用mapper
        Category category = new Category();
        category.setId(id);
        category.setType(count);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.deleteById(category);
        return Result.success();
    }

    /**
     * 新增分类
     *
     * @param dto
     * @return
     */
    @Override
    public Result save(CategoryDTO dto) {
        //判断非空
        if (dto.getName() == null) {
            return Result.error(MessageConstant.ACCOUNT_NULL);
        }
        if (dto.getSort() == null) {
            return Result.error(MessageConstant.PAIXU_NOT);
        }

        //(调用mapper)根据id去查询数据库是否存在，如果存在，则直接返回错误信息
        Category category = categoryMapper.getByName(dto.getName());
        if (category != null) {
            return Result.error(MessageConstant.ACCOUNT_EXISTS_FOUND);
        }
        category = new Category();
        //对象属性拷贝（ ）
        BeanUtils.copyProperties(dto, category);
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setCreateTime(LocalDateTime.now());
        categoryMapper.insert(category);
        return Result.success();
    }

    /**
     * 修改数据
     *
     * @param dto
     * @return
     */
    @Override
    public Result updateCategory(CategoryDTO dto) {
        //判断非空
        if (dto.getId() == null) {
            return Result.error(MessageConstant.ACCOUNT_NULL);
        }
        //调用mapper
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.updateStatus(category);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public Result<List<Category>> list(Integer type) {
        List<Category> list = categoryMapper.list(type);
        return Result.success(list);
    }


}
